package serverproject;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import serverproject.model.Account;
import serverproject.service.AccountService;
import serverproject.service.BanListService;
import serverproject.service.ConnectionService;

public class Server
{
	public static final boolean	useTestLog	= true; // 테스트용 로그 컨트롤

	public Server()
	{
		ServerSocket serverSocket = null;

		try
		{
			serverSocket = new ServerSocket(9002);
			while (true)
			{
				Socket socket = serverSocket.accept();

				ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
				ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
				Object object = ois.readObject();
				Account account = null;

				if (object instanceof Account)
				{
					account = (Account) object;
					if (AccountService.getInstance().login(account.getId(), account.getPassword()) != null)
						oos.writeObject(AccountService.getInstance().login(account.getId(), account.getPassword()));
				}

				Thread thread = new PerClientThread(socket);
				thread.start();
			}
		} catch (Exception e)
		{
			System.out.println(e.getMessage());
		}

	}

	public static void main(String args[])
	{
		ConnectionService.getInstance();
		BanListService.getInstance();
		AccountService.getInstance();

		new Server();
	}
}
